<?php
namespace Dashboard\Controller;
class TeacherController extends BaseController{
    public function index(){
        $a        = (int)(I("currpage"));//跳转页数
        $sou      = I('sou');
        $pagesize = 20;
        if($a != null){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        if($sou){
            $content = I('content');
            $totalpage = 1;
            $currpage  = 1;
            if($sou == 1){
                $list      = sels("teacher","","name like '%{$content}%'","id desc");
            }
        }else{
            $result = currpage("teacher","",$pagesize,$currpage,"id desc");
            $totalpage = $result['totalpage'];
            $list      = $result['list'];
            if($result['currpage']){
                $currpage = $result['currpage'];
            }
        }
        if($list){
            /*foreach ($list as $k=>$v){
                $teacher = sel("teacher","name","id = {$v['teacher_id']}");
                $list[$k]['teacher'] = $teacher['name'];
                //视频课程
                $vedios = sels("voices","","voice_zhu_id = {$v['id']}");
                if($vedios){
                    $list[$k]['vedios'] = $vedios;
                }
            }*/
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    public function form(){
        $id = I('id');
        $currpage = I('currpage');
        $this->assign("currpage",$currpage);
        if($id){
            $list = sel("teacher"," * ",array("id"=>$id));
            if($list['tags']){
                $ta = explode("/",$list['tags']);
                $tags_b   = sel("teacher_tags","content");
                $tagss    = explode("/",$tags_b['content']);
                for ($i = 0;$i<count($tagss);$i++){
                    if(in_array($tagss[$i],$ta)){
                        $tags[$i]['choose'] = true;
                    }else{
                        $tags[$i]['choose'] = false;
                    }
                    $tags[$i]['name'] = $tagss[$i];
                }
                $list['tags'] = $tags;
            }else{
                $tags_b   = sel("teacher_tags","content");
                $tagss    = explode("/",$tags_b['content']);
                for ($i = 0;$i<count($tagss);$i++){
                    $tags[$i]['choose'] = false;
                    $tags[$i]['name'] = $tagss[$i];
                }
                $list['tags'] = $tags;
            }
        }else{
            $tags_b   = sel("teacher_tags","content");
            $tagss    = explode("/",$tags_b['content']);
            for ($i = 0;$i<count($tagss);$i++){
                $tags[$i]['choose'] = false;
                $tags[$i]['name'] = $tagss[$i];
            }
            $list['tags'] = $tags;
        }
        $this->assign("list",$list);
        $this->display();
    }
    public function add(){
        $tags           = I('tags');
        if($tags){
            $data['tags'] =  implode("/",$tags);
        }
        $data['name'] = I('name');
        $data['moneys'] = I('moneys');
        $data['intro'] = I('intro');
        $data['phone'] = I('phone');
        $data['tui'] = I('tui');
        $id = I('id');
        //处理图片
        if ($_FILES['one']['name'] != null) {
            $img = I('banner');
            unlink($img);
            $info = $this->imgUpload("teacher/");
            $savename = $info['one']['savename'];
            $savepath = $info['one']['savepath'];
            $data['banner'] = "Data/" . $savepath . $savename;
        }
        unset($_FILES['one']);
        if ($_FILES['two']['name'] != null) {
            $img = I('avatar');
            unlink($img);
            $info = $this->imgUpload("teacher/");
            $savename = $info['two']['savename'];
            $savepath = $info['two']['savepath'];
            $data['avatar'] = "Data/" . $savepath . $savename;
        }
        if ($id) {
            $data['up_at'] = time();
            $res = up_one("teacher","id = {$id}",$data);
            if($res){
                $this->success("修改成功",U("index"));
            }else{
                $this->success("修改成功",U("form"));
            }
        }else{
            $data['create_at'] = time();
            $res = ins("teacher",$data);
            if($res){
                $this->success("添加成功",U("form"));
            }else{
                $this->success("添加失败",U("form"));
            }
        }
    }

    //用户标签
    public function tags(){
        $list      = sels("teacher_tags");
        $this->assign("list",$list);
        $this->display();
    }
    public function tags_form(){
        $id      = I('id');
        if($id != null){
            $lists       = sel("teacher_tags","",array("id"=>$id));
            $this->assign("list",$lists);
        }
        $this->display();
    }
    public function tags_add(){
        $id               = $_POST['id'];
        $data['content'] = I('content');
        if($id == null){
            $data['create_at'] = time();
            $result             = ins("teacher_tags",$data);
            if($result){
                $logs = logs("添加了用户标签信息");
                $this->success("添加成功",U('tags_form'));}
        }else{
            $data['up_at']     = time();
            $result = up_one("teacher_tags",array("id"=>$id),$data);
            if($result){
                $logs = logs("修改了用户标签信息");
                $this->success("修改成功",U('tags'));}else{$this->success("没有信息变更，修改失败");}
        }
    }
}